// this file requires jquery
var bioventurist_dir = '/base/bioventurist/';
var bioventurist_dir_old = '/user/bioventurist/default_domain/';

function generalSearchSuggest(){

   jQuery(function($) {
        var options = {
            soft: true,
            //suggest_new: 'Create new biotechnology company',
            ac_param: {
                type: [bioventurist_dir + "science_or_technology_company",
                    bioventurist_dir + "bv_medical_condition",
                    bioventurist_dir + "product",
                    bioventurist_dir + "technology_class",
                    bioventurist_dir + "bv_research_group",
                    bioventurist_dir + "bv_venture_investor",
                    bioventurist_dir + "biomolecule"],                 
                //strict: "any",
                start: 0,
                limit: 30
            }
        };
        $('#searchSuggest').freebaseSuggest(options)
        .bind('fb-select', function(e, data) { 
        	var nextDir = searchSelect_results(data);
        	location.href = nextDir;
        });
        //.bind('fb-select-new', function(e, data) { console.log('suggest new: ', data.name); });
        $('#searchSuggest').focus(function(e) { $(this).val(""); })
        .blur(function(e) { if ($(this).val() == "") $(this).val('search'); })
    });
}

/* Directs a search result to the correct page */
function searchSelect_results(data) { 
	var id_s = data.id;

	var types = data.type;
	//name = name.replace('&amp;','%26');
	
	var nextDir = '';
	
	// First need to test if it is a product sub-type:
	for (var i in types){
		var type = types[i].id_s;
		if (type == bioventurist_dir+"bv_therapeutic" || type == bioventurist_dir_old+"bv_therapeutic") {
        	return ('/therapeutic' + id_s); 
        }
	}
	
	for (var i in types){
		var type = types[i].id;
		if (type == bioventurist_dir+ "science_or_technology_company" || type == bioventurist_dir_old+ "science_or_technology_company"  ){
			return ('/company' + id_s); 
		} else if (type == bioventurist_dir+"bv_medical_condition" || type == bioventurist_dir_old+"bv_medical_condition") {
        	return ('/disease' + id_s); 
		}  else if (type == bioventurist_dir+"product" || type == bioventurist_dir_old+"product") {
        	return ('/product' + id_s); 
        } else if (type == bioventurist_dir+"technology_class" || type == bioventurist_dir_old+"technology_class") {
        	return ('/technology' + id_s); 
        } else if (type == bioventurist_dir+"bv_research_group" || type == bioventurist_dir_old+"bv_research_group") {
        	return ('/researchgroup' + id_s);
        } else if (type == bioventurist_dir+"bv_venture_investor" || type == bioventurist_dir_old+"bv_venture_investor"){
        	return ('/vcfirm' + id_s);
        } else if (type == bioventurist_dir+"biomolecule" || type == bioventurist_dir_old+"biomolecule") {
        	return ('/biomolecule' + id_s);
        }
	}
	
	return nextDir;
}
